package com.git.smp.controller;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.dao.EsbEnvironmentMapper;
import com.git.smp.dao.RequirementBatchMapper;
import com.git.smp.dao.SmpFieldMapper;
import com.git.smp.dao.SmpSystemCallMapper;
import com.git.smp.entity.EsbEnvironment;
import com.git.smp.entity.MappingNameInfo;
import com.git.smp.entity.Release;
import com.git.smp.entity.RequirementBatch;
import com.git.smp.entity.SmpField;
import com.git.smp.entity.SmpSystemCall;
import com.git.smp.entity.SqlInfo;
import com.git.smp.utils.InitMappingFile;
import com.git.smp.utils.InitSql;
import com.git.smp.utils.SftpChannel;
import com.git.smp.utils.StringUtil;
import com.git.smp.utils.XqueryUtil;
import com.git.smp.utils.XsdUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

import freemarker.template.Configuration;
import freemarker.template.Template;

@RestController
@RequestMapping("/release")
public class ReleaseController {

	Logger logger = LoggerFactory.getLogger(ReleaseController.class);

	@Autowired
	private Configuration configuration;

	@Autowired
	SmpSystemCallMapper smpSystemCallMapper;

	@Autowired
	private HttpServletResponse response;

	@Autowired
	private SmpFieldMapper smpFieldMapper;

	@Autowired
	EsbEnvironmentMapper environmentMapper;

	@Autowired
	RequirementBatchMapper requirementBatchMapper;

	@RequestMapping("/initFile")
	public void initSql(@RequestBody Release release) {

		// 保存内联配置
		RequirementBatch requirementBatch = new RequirementBatch();
		requirementBatch.setQmsNo(release.getQmsNo());
		requirementBatch.setBatchNum(release.getBatchNum());
		requirementBatch.setEsbSource(release.getEsbSource());
		requirementBatch.setEsbFile(release.getEsbFile() == null ? null : release.getEsbFile().getBytes());
		requirementBatchMapper.updateByPrimaryKeySelective(requirementBatch);

		SmpSystemCall smpSystemCall = new SmpSystemCall();
		smpSystemCall.setAssociatedRequirement(release.getQmsNo());
		smpSystemCall.setBatchNum(release.getBatchNum());

		StringBuffer fileList = new StringBuffer();

		response.setContentType("application/octet-stream;charset=UTF-8");

		OutputStream out = null;
		ZipOutputStream zip = null;

		try {
			out = response.getOutputStream();
			zip = new ZipOutputStream(out);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<SqlInfo> listSqlInfo = smpSystemCallMapper.getSqlInfoList(smpSystemCall);
		StringBuffer sb = new StringBuffer();
		for (SqlInfo sqlInfo : listSqlInfo) {
			InitSql.createSql(sqlInfo, sb);
		}

		try {

			ZipEntry zipEntry;
			if ("01".equals(release.getBatchNum())) {
				zipEntry = new ZipEntry("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_ESB.sql");
				fileList.append("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_ESB.sql\n");

			} else {
				zipEntry = new ZipEntry("database/" + release.getQmsNo() + "_"
						+ release.getBatchNum() +  "/" + release.getQmsNo() + "_"
						+ release.getBatchNum() + "_ESB.sql");
				fileList.append("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_"
						+ release.getBatchNum() + "_ESB.sql\n");

			}

			/*
			 * ZipEntry zipEntry = new ZipEntry("database/" + release.getQmsNo() + "/" +
			 * release.getQmsNo() + ".sql"); fileList.append("database/" +
			 * release.getQmsNo() + "/" + release.getQmsNo() + ".sql\n");
			 */
			zip.putNextEntry(zipEntry);
			zip.write(sb.toString().getBytes());
			zip.closeEntry();

		} catch (Exception e) {
			e.printStackTrace();
		}

		List<MappingNameInfo> listMappingNameInfo = smpSystemCallMapper.getMappingNameInfoList(smpSystemCall);
		SmpField smpField = new SmpField();
		for (MappingNameInfo mappingNameInfo : listMappingNameInfo) {
			smpField.setSystemNo(mappingNameInfo.getSystemNo());
			smpField.setTransCode(mappingNameInfo.getTransCode());
			List<SmpField> listField = smpFieldMapper.getFieldListByNoCode(smpField);
			Template template = null;
			if (fileList.indexOf("esbyx/Config/mappingFile/" + mappingNameInfo.getSystemShort() + "/"
					+ mappingNameInfo.getSystemShort() + "_" + mappingNameInfo.getPubCode() + ".xml\n") < 0) {
				try {
					fileList.append("esbyx/Config/mappingFile/" + mappingNameInfo.getSystemShort() + "/"
							+ mappingNameInfo.getSystemShort() + "_" + mappingNameInfo.getPubCode() + ".xml\n");
					Document doc = InitMappingFile.createMappingFile(listField, mappingNameInfo);
					OutputFormat format = OutputFormat.createPrettyPrint();
					// 设置输出编码
					format.setEncoding("UTF-8");
					// 去除空行
					format.setNewLineAfterDeclaration(false);

					// 输出文件
					// File file = new File(tarPath +File.separator+ system + "_" + pubSrvCode +
					// ".xml");
					XMLWriter writer;

					ZipEntry zipEntry = new ZipEntry("esbyx/Config/mappingFile/" + mappingNameInfo.getSystemShort()
							+ "/" + mappingNameInfo.getSystemShort() + "_" + mappingNameInfo.getPubCode() + ".xml");
					zip.putNextEntry(zipEntry);
					writer = new XMLWriter(zip, format);
					writer.write(doc);
					// writer.close();
					zip.closeEntry();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			if (fileList.indexOf("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort() + "/SRV_"
					+ mappingNameInfo.getPubCode() + "_" + mappingNameInfo.getSystemShort()
					+ mappingNameInfo.getTransCode() + "Srv_Def" + ".xsd\n") < 0) {
				// 生成xsd文件
				try {
					Document doc1 = XsdUtil.createSchema(listField, mappingNameInfo.getBusCode(),
							mappingNameInfo.getSystemShort());

					OutputFormat format1 = OutputFormat.createPrettyPrint();
					// 设置输出编码
					format1.setEncoding("UTF-8");
					// 去除空行
					format1.setSuppressDeclaration(true);

					// 输出文件
					// File file = new File(tarPath +File.separator+ system + "_" + pubSrvCode +
					// ".xml");
					XMLWriter writer1;
					ZipEntry zipEntry1 = new ZipEntry("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort()
							+ "/SRV_" + mappingNameInfo.getPubCode() + "_" + mappingNameInfo.getSystemShort()
							+ mappingNameInfo.getTransCode() + "Srv_Def" + ".xsd");

					fileList.append("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort() + "/SRV_"
							+ mappingNameInfo.getPubCode() + "_" + mappingNameInfo.getSystemShort()
							+ mappingNameInfo.getTransCode() + "Srv_Def" + ".xsd\n");

					try {
						zip.putNextEntry(zipEntry1);
						writer1 = new XMLWriter(zip, format1);
						writer1.write(doc1);
						// writer.close();
						zip.closeEntry();

					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (fileList.indexOf("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort() + "/SRV_"
					+ mappingNameInfo.getPubCode() + "_WSDL.wsdl") < 0) {
				// 生成wsdl文件
				ZipEntry zipEntry1 = new ZipEntry("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort() + "/SRV_"
						+ mappingNameInfo.getPubCode() + "_WSDL.wsdl");
				fileList.append("esbyx/AHRCUApp/WSDL/" + mappingNameInfo.getSystemShort() + "/SRV_"
						+ mappingNameInfo.getPubCode() + "_WSDL.wsdl\n");

				try {
					template = configuration.getTemplate("wsdl.ftl");
				} catch (IOException e1) {
					logger.error("", e1);
				}

				Map<String, Object> params = new HashMap<>();
				params.put("pubCode", mappingNameInfo.getPubCode());
				params.put("busCode", mappingNameInfo.getBusCode());
				params.put("operation", mappingNameInfo.getSystemShort() + mappingNameInfo.getTransCode());
				try {
					zip.putNextEntry(zipEntry1);
					Writer writer1 = new OutputStreamWriter(zip);
					template.process(params, writer1);
					zip.closeEntry();
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("", e);
				}

			}

			if (fileList.indexOf(
					"esbyx/AHRCUApp/XQuery/\"+mappingNameInfo.getSystemShort()+\"/SOAP/SRV_\"+mappingNameInfo.getPubCode()+\"_ReqMap.xq") < 0) {
				// 生成xquery（请求方soap）

				ZipEntry zipEntry2 = new ZipEntry("esbyx/AHRCUApp/XQuery/" + mappingNameInfo.getSystemShort()
						+ "/SOAP/SRV_" + mappingNameInfo.getPubCode() + "_ReqMap.xq");
				fileList.append("esbyx/AHRCUApp/XQuery/" + mappingNameInfo.getSystemShort() + "/SOAP/SRV_"
						+ mappingNameInfo.getPubCode() + "_ReqMap.xq\n");

				try {
					template = configuration.getTemplate("requestClientSoap.ftl");
				} catch (IOException e1) {
					logger.error("", e1);
				}

				Map<String, Object> xqueryparams = new HashMap<>();
				String bizHeader = XqueryUtil.createDoc(listField, "01", "01", "xmlBodyReq");
				String bizBizBody = XqueryUtil.createDoc(listField, "02", "01", "xmlBodyReq");

				xqueryparams.put("bizHeader", bizHeader);
				xqueryparams.put("bizBody", bizBizBody);
				xqueryparams.put("pubCode", mappingNameInfo.getPubCode());
				xqueryparams.put("systemShort", mappingNameInfo.getSystemShort());
				xqueryparams.put("busCode", mappingNameInfo.getBusCode());

				try {
					zip.putNextEntry(zipEntry2);
					Writer writer1 = new OutputStreamWriter(zip);
					template.process(xqueryparams, writer1);
					zip.closeEntry();
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("", e);
				}
			}

			if (fileList.indexOf(
					"esbyx/AHRCUApp/XQuery/\"+mappingNameInfo.getSystemShort()+\"/SOAP/SRV_\"+mappingNameInfo.getPubCode()+\"_ResMap.xq") < 0) {
				ZipEntry zipEntry2 = new ZipEntry("esbyx/AHRCUApp/XQuery/" + mappingNameInfo.getSystemShort()
						+ "/SOAP/SRV_" + mappingNameInfo.getPubCode() + "_ResMap.xq");
				fileList.append("esbyx/AHRCUApp/XQuery/" + mappingNameInfo.getSystemShort() + "/SOAP/SRV_"
						+ mappingNameInfo.getPubCode() + "_ResMap.xq\n");

				try {
					template = configuration.getTemplate("responseClientSoap.ftl");
				} catch (IOException e1) {
					logger.error("", e1);
				}

				Map<String, Object> xqueryparams = new HashMap<>();
				String bizHeader = XqueryUtil.createDoc(listField, "01", "02", "xmlResp");
				String bizBizBody = XqueryUtil.createDoc(listField, "02", "02", "xmlResp");

				xqueryparams.put("bizHeader", bizHeader);
				xqueryparams.put("bizBody", bizBizBody);
				xqueryparams.put("pubCode", mappingNameInfo.getPubCode());
				xqueryparams.put("systemShort", mappingNameInfo.getSystemShort());
				xqueryparams.put("busCode", mappingNameInfo.getBusCode());
				try {
					zip.putNextEntry(zipEntry2);
					Writer writer1 = new OutputStreamWriter(zip);
					template.process(xqueryparams, writer1);
					zip.closeEntry();
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("", e);
				}
			}

		}

		

		/*
		 * //读取清单文件
		 * 
		 * Reader reader = null; try { reader = new
		 * InputStreamReader(file.getInputStream()); } catch (IOException e1) { // TODO
		 * Auto-generated catch block e1.printStackTrace(); } BufferedReader
		 * bufferedReader = new BufferedReader(reader); String line;
		 */

		if (!StringUtil.isBlank(release.getEsbFile())) {

			InputStream fin = new ByteArrayInputStream(release.getEsbFile().getBytes());
			Reader read = new InputStreamReader(fin);
			BufferedReader bufferedReader = new BufferedReader(read);
			try {

				EsbEnvironment esbEnvironment = environmentMapper.selectByPrimaryKey(release.getEsbSource());
				SftpChannel sftpChannel = new SftpChannel();
				ChannelSftp sftp = sftpChannel.connectByPwd(esbEnvironment.getIp(),
						Integer.parseInt(esbEnvironment.getPort()), esbEnvironment.getUserName(),
						esbEnvironment.getPasswd());
				String line;
				while ((line = bufferedReader.readLine()) != null) {
					String fileName = line.replace(".biz", ".business.xml").replace(".proxy", ".proxy.xml")
							.replace(".xq", ".xquery").replace(".jar", ".ar");
					logger.info(fileName);
					InputStream in = sftp.get(esbEnvironment.getContentPath() + fileName);

					if (line.indexOf(".xq") > -1 || line.indexOf(".xsd") > -1 || line.indexOf(".wsdl") > -1) {
						ByteArrayOutputStream bout = new ByteArrayOutputStream();

						byte[] b = new byte[1024];
						int len = -1;
						while ((len = in.read(b)) != -1) {
							bout.write(b, 0, len);
						}

						String value = truncFile(bout.toString("UTF-8"), line);
						ZipEntry zipEntry = new ZipEntry(line);
						fileList.append(line + "\n");

						zip.putNextEntry(zipEntry);
						zip.write(value.getBytes("UTF-8"));
						in.close();
						zip.closeEntry();
					} else {
						ZipEntry zipEntry = new ZipEntry(line);
						fileList.append(line + "\n");
						zip.putNextEntry(zipEntry);

						byte[] b = new byte[1024];
						int len = -1;
						while ((len = in.read(b)) != -1) {
							zip.write(b, 0, len);
						}
						in.close();
						zip.closeEntry();
					}
				}
				/*
				 * while(( line = bufferedReader.readLine())!=null) { String fileName =
				 * line.replace(".biz", ".business.xml").replace(".proxy",
				 * ".proxy.xml").replace(".xq", ".xquery"); logger.info(fileName); InputStream
				 * in = sftp.get(
				 * "/home/esb/Oracle/Middleware/user_projects/domains/osb_domain/osb/config/core/"
				 * +fileName);
				 * 
				 * 
				 * if(line.indexOf(".xq")>-1 || line.indexOf(".xsd")>-1
				 * ||line.indexOf(".wsdl")>-1) { ByteArrayOutputStream bout = new
				 * ByteArrayOutputStream();
				 * 
				 * byte[] b = new byte[1024]; int len =-1; while((len = in.read(b))!=-1) {
				 * bout.write(b, 0, len); }
				 * 
				 * String value = truncFile(bout.toString("UTF-8"), line);
				 * 
				 * ZipEntry zipEntry = new ZipEntry(line);
				 * 
				 * zip.putNextEntry(zipEntry); zip.write(value.getBytes("UTF-8")); in.close();
				 * zip.closeEntry(); }else { ZipEntry zipEntry = new ZipEntry(line);
				 * 
				 * zip.putNextEntry(zipEntry);
				 * 
				 * byte[] b = new byte[1024]; int len =-1; while((len = in.read(b))!=-1) {
				 * zip.write(b, 0, len); } in.close(); zip.closeEntry(); } }
				 * bufferedReader.close();
				 */
				sftpChannel.closeChannel();

				/* logger.info(line); */

			} catch (IOException e1) {

				e1.printStackTrace();
			} catch (JSchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SftpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {
			ZipEntry zipEntry = new ZipEntry("file_list.txt");
			zip.putNextEntry(zipEntry);
			byte[] b = fileList.toString().getBytes("UTF-8");
			zip.write(b, 0, b.length - 1);
			zip.closeEntry();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			zip.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		logger.info("生成文件成功！");
	}

	@RequestMapping("/initOESBFile")
	public void initOESBFile(@RequestBody Release release) {

		// 保存外联配置
		RequirementBatch requirementBatch = new RequirementBatch();
		requirementBatch.setQmsNo(release.getQmsNo());
		requirementBatch.setBatchNum(release.getBatchNum());
		requirementBatch.setOesbSource(release.getOesbSource());
		requirementBatch.setOesbFile(release.getOesbFile() == null ? null : release.getOesbFile().getBytes());
		requirementBatchMapper.updateByPrimaryKeySelective(requirementBatch);

		SmpSystemCall smpSystemCall = new SmpSystemCall();
		smpSystemCall.setAssociatedRequirement(release.getQmsNo());
		smpSystemCall.setBatchNum(release.getBatchNum());

		response.setContentType("application/octet-stream;charset=UTF-8");
		StringBuffer fileList = new StringBuffer();
		OutputStream out = null;
		ZipOutputStream zip = null;

		try {
			out = response.getOutputStream();
			zip = new ZipOutputStream(out);

		} catch (Exception e) {
			e.printStackTrace();
		}

		List<SqlInfo> listSqlInfo = smpSystemCallMapper.getSqlInfoList(smpSystemCall);
		StringBuffer sb = new StringBuffer();
		for (SqlInfo sqlInfo : listSqlInfo) {
			if (sqlInfo.getOesbLocalPath() != null && !"".equals(sqlInfo.getOesbLocalPath())) {
				InitSql.createOESBSql(sqlInfo, sb);
			}
		}

		try {
			ZipEntry zipEntry;
			if ("01".equals(release.getBatchNum())) {
				zipEntry = new ZipEntry("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_OESB.sql");
				fileList.append("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_OESB.sql\n");

			} else {
				zipEntry = new ZipEntry("database/" + release.getQmsNo() + "_"
						+ release.getBatchNum() + "/" + release.getQmsNo() + "_"
						+ release.getBatchNum() + "_OESB.sql");
				fileList.append("database/" + release.getQmsNo() + "/" + release.getQmsNo() + "_"
						+ release.getBatchNum() + "_OESB.sql\n");

			}

			zip.putNextEntry(zipEntry);
			zip.write(sb.toString().getBytes());
			zip.closeEntry();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 读取清单文件

		/*
		 * Reader reader = null; try { reader = new
		 * InputStreamReader(file.getInputStream()); } catch (IOException e1) { // TODO
		 * Auto-generated catch block e1.printStackTrace(); } BufferedReader
		 * bufferedReader = new BufferedReader(reader); String line;
		 */

		if (!StringUtil.isBlank(release.getOesbFile())) {

			InputStream fin = new ByteArrayInputStream(release.getOesbFile().getBytes());
			Reader read = new InputStreamReader(fin);
			BufferedReader bufferedReader = new BufferedReader(read);

			// String[] line = release.getFile().split("\n");
			try {

				EsbEnvironment esbEnvironment = environmentMapper.selectByPrimaryKey(release.getOesbSource());
				SftpChannel sftpChannel = new SftpChannel();
				ChannelSftp sftp = sftpChannel.connectByPwd(esbEnvironment.getIp(),
						Integer.parseInt(esbEnvironment.getPort()), esbEnvironment.getUserName(),
						esbEnvironment.getPasswd());
				String line;
				while ((line = bufferedReader.readLine()) != null) {
					String fileName = line.replace(".biz", ".business.xml").replace(".proxy", ".proxy.xml")
							.replace(".xq", ".xquery").replace(".jar", ".ar");
					logger.info(fileName);
					InputStream in = sftp.get(esbEnvironment.getContentPath() + fileName);

					if (line.indexOf(".xq") > -1 || line.indexOf(".xsd") > -1 || line.indexOf(".wsdl") > -1) {
						ByteArrayOutputStream bout = new ByteArrayOutputStream();

						byte[] b = new byte[1024];
						int len = -1;
						while ((len = in.read(b)) != -1) {
							bout.write(b, 0, len);
						}

						String value = truncFile(bout.toString("UTF-8"), line);
						ZipEntry zipEntry = new ZipEntry(line);
						fileList.append(line + "\n");

						zip.putNextEntry(zipEntry);
						zip.write(value.getBytes("UTF-8"));
						in.close();
						zip.closeEntry();
					} else {
						ZipEntry zipEntry = new ZipEntry(line);
						fileList.append(line + "\n");
						zip.putNextEntry(zipEntry);

						byte[] b = new byte[1024];
						int len = -1;
						while ((len = in.read(b)) != -1) {
							zip.write(b, 0, len);
						}
						in.close();
						zip.closeEntry();
					}
				}
				/*
				 * while(( line = bufferedReader.readLine())!=null) { String fileName =
				 * line.replace(".biz", ".business.xml").replace(".proxy",
				 * ".proxy.xml").replace(".xq", ".xquery"); logger.info(fileName); InputStream
				 * in = sftp.get(
				 * "/home/esb/Oracle/Middleware/user_projects/domains/osb_domain/osb/config/core/"
				 * +fileName);
				 * 
				 * 
				 * if(line.indexOf(".xq")>-1 || line.indexOf(".xsd")>-1
				 * ||line.indexOf(".wsdl")>-1) { ByteArrayOutputStream bout = new
				 * ByteArrayOutputStream();
				 * 
				 * byte[] b = new byte[1024]; int len =-1; while((len = in.read(b))!=-1) {
				 * bout.write(b, 0, len); }
				 * 
				 * String value = truncFile(bout.toString("UTF-8"), line);
				 * 
				 * ZipEntry zipEntry = new ZipEntry(line);
				 * 
				 * zip.putNextEntry(zipEntry); zip.write(value.getBytes("UTF-8")); in.close();
				 * zip.closeEntry(); }else { ZipEntry zipEntry = new ZipEntry(line);
				 * 
				 * zip.putNextEntry(zipEntry);
				 * 
				 * byte[] b = new byte[1024]; int len =-1; while((len = in.read(b))!=-1) {
				 * zip.write(b, 0, len); } in.close(); zip.closeEntry(); } }
				 * bufferedReader.close();
				 */
				sftpChannel.closeChannel();

				// logger.info(line);

			} catch (IOException e1) {

				e1.printStackTrace();
			} catch (JSchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SftpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		try {
			ZipEntry zipEntry = new ZipEntry("file_list.txt");
			zip.putNextEntry(zipEntry);
			byte[] b = fileList.toString().getBytes("UTF-8");
			zip.write(b, 0, b.length - 1);
			zip.closeEntry();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			zip.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		logger.info("生成文件成功！");
	}

	public String truncFile(String value, String line) {
		String result = "";
		try {
			Document doc = DocumentHelper.parseText(value);
			Element root = doc.getRootElement();
			Element element = null;
			if (line.indexOf(".xq") > -1) {
				element = root.element("xquery");
			} else if (line.indexOf(".xsd") > -1) {
				element = root.element("schema");
			} else if (line.indexOf(".wsdl") > -1) {
				element = root.element("wsdl");
			}
			result = element.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
